<template>
  <div class="image-preview">
    <div class="image-container" v-loading="loading">
      <img 
        v-if="fileInfo.fileUrl" 
        :src="fileInfo.fileUrl" 
        :alt="fileInfo.fileName"
        @load="handleImageLoad"
        @error="handleImageError"
        class="preview-image"
      />
      <div v-else-if="error" class="error-message">
        <el-icon><Picture /></el-icon>
        <p>图片加载失败</p>
      </div>
    </div>
    
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Picture } from '@element-plus/icons-vue'

const props = defineProps({
  fileInfo: {
    type: Object,
    default: () => ({})
  }
})

const loading = ref(true)
const error = ref(false)

// 图片加载完成
const handleImageLoad = (event) => {
  loading.value = false
  error.value = false
}

// 图片加载错误
const handleImageError = () => {
  error.value = true
  loading.value = false
}

onMounted(() => {
  console.log(props.fileInfo);
  // 初始化时设置加载状态
  if (props.fileInfo.fileUrl) {
    loading.value = true
    error.value = false
  }
})
</script>

<style scoped>
.image-preview {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.image-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border-radius: 8px;
  min-height: 400px;
  position: relative;
}

.preview-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.error-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 14px;
}

.error-message .el-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

</style>
